package handler

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/interface/dao"
	"golang-websocket-chatroom/interface/models"
)

func ChatCreate(context *gin.Context) {
	var req request.ChatReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var chat models.Chats
	dao.Groups(&chat, &models.Chats{
		Own:     context.GetUint("userId"),
		Other:   req.Other,
		Context: req.Context,
		Status:  1,
	})

	if chat.Status == 1 {
		dao.Updates(&models.Chats{
			Status: chat.Status,
		})
	}
	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "同意",
		"data": nil,
	})

}
